package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.datasource.DynamicDataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.tenant.domain.SysTenant;
import com.ruoyi.tenant.service.ISysTenantService;
import com.ruoyi.tenant.utils.AddDataSourceUtil;
import com.ruoyi.yuangong.domain.Yuangong;
import com.ruoyi.yuangong.service.IYuangongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import javax.sql.DataSource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private AddDataSourceUtil addDataSourceUtil;

    @Autowired
    private ISysTenantService iSysTenantService;

    @Autowired
    private IYuangongService iYuangongService;

    @Value("${tenant.expire}")
    private Integer expireDay;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    //租户定时方法测试
    public void testInsertTenant(String tenantId,String params)
    {
        //获取连接池中所有链接
        DynamicDataSource dynamicDataSource = SpringUtils.getBean("dynamicDataSource");
        Map<Object, DataSource> resolvedDataSourcesMap = dynamicDataSource.getResolvedDataSources();
        Set<Object> dynamicDataSourceSet = resolvedDataSourcesMap.keySet();

        //若数据连接池没有则添加
        if (!dynamicDataSourceSet.contains(tenantId)) {
            SysTenant sysTenant = iSysTenantService.selectTenantByTenantId(tenantId);
            addDataSourceUtil.addDataSource(sysTenant);
        }
        //设置当前租户对应的数据库
        DynamicDataSourceContextHolder.setDataSourceType(tenantId);
        Yuangong yuangong = new Yuangong();
        yuangong.setDate(new Date());
        yuangong.setDianming("测试");
        yuangong.setRenshu(5L);
        yuangong.setBeizhu("定时任务");
        iYuangongService.insertYuangong(yuangong);
        DynamicDataSourceContextHolder.clearDataSourceType();
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    //定时停用租户方法
    public void disTenant()
    {
        DynamicDataSourceContextHolder.setDataSourceType("master");
        SysTenant tenant = new SysTenant();
        tenant.setEmpower(0);
        tenant.setState(0);
        List<SysTenant> listTenant = iSysTenantService.selectSysTenantList(tenant);
        for (SysTenant itemTenant:listTenant) {
            if( DateUtils.differentDaysByMillisecond(new Date(),itemTenant.getCreateDate())>expireDay){
                itemTenant.setState(1);
                itemTenant.setChangePsd(false);
                iSysTenantService.updateSysTenant(itemTenant);
                System.out.println("执行停用租户方法：" + itemTenant.getTenantid());
            }
        }
        DynamicDataSourceContextHolder.clearDataSourceType();
    }

}
